#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>





const char* getusername()
{
  return getenv("USER");
}

const char* gethtname()
{
  return getenv("HOSTNAME");
}

char pwd[128];
char* getpwd()
{
  return getcwd(pwd, sizeof(pwd));
}



int main()
{
  int lastcode = 0;
  char commandline[32];
  char* argv[32];
  while(1){
  //init
  printf("[%s@%s %s]# ", getusername(), gethtname(), getpwd());
  fgets(commandline, sizeof(commandline), stdin);
//  printf("%d", strlen(commandline));
  commandline[strlen(commandline) - 1] = '\0';
  //printf("%s", commandline);


  //split
  int i = 0;
  argv[i++] = strtok(commandline, " ");
  while(argv[i++] = strtok(NULL, " "));
  int argc = i - 1;
  if(!argc) continue;
  //for(int j = 0;argv[j]; j++)
  //{
  //  printf("%s\n", argv[j]);
  //}
  

  //internal
  



  //normal
  pid_t id = fork();
  if(id < 0)
  {
    perror("fork");
    continue;
  }
  else if(id == 0)
  {
    //child
    execvp(argv[0], argv);
    
    exit(44);
  }

  else
  {
    //parent
    int status = 0;
    pid_t ret = waitpid(id, &status, 0);
    if(ret < 0)
    {
      printf("wait failed\n");
    }
    else
    {
      lastcode = WEXITSTATUS(status);
    }

  }
  }
  return 0;
}
